package com.hc;

import com.sun.org.apache.bcel.internal.generic.SIPUSH;
import org.junit.Test;

import java.io.PrintStream;
import java.util.Comparator;
import java.util.function.*;

/**
 * 方法引用
 * 构造器引用
 * 数组引用
 */
public class TestMethodRef {


    /**
     * 数组引用
     */

    @Test
    public void test7(){

        Function<Integer,String[]> fun = (x) -> new String[x];
        String[] apply = fun.apply(10);
        System.out.println(apply.length);

        System.out.println("---------------------------------------------");

        Function<Integer,String[]> fun2 = String[]::new;
        String[] apply2 = fun.apply(20);
        System.out.println(apply2.length);


    }



    /**
     * 构造器引用
     */
    @Test
    public void test5(){
        Supplier<Employee> sup =() -> new Employee();

        //构造器引用的方式
        //通过接口来匹配的构造方法
        Supplier<Employee> sup2 = Employee::new;
    }

    @Test
    public void test6(){
        Function<Integer,Employee> fun = (x) -> new Employee(x);

        Function<Integer,Employee> fun2 = Employee::new;
        Employee apply = fun2.apply(101);
        System.out.println(apply);

        System.out.println("-------------------------------------------------------");

        BiFunction<Integer,String,Employee> bf3 = Employee::new;
        Employee apply1 = bf3.apply(18, "老王");
        System.out.println(apply1);
    }


    /**
     * 类::实例方法名
     */
    @Test
    public void test4(){
        BiPredicate<String,String> bp =(x,y) -> x.equals(y);

        BiPredicate<String,String> bp2 = String::equals;
    }



    /**
     * 类:静态方法名
     */
    @Test
    public void test3(){
        Comparator<Integer> com = (x,y) -> Integer.compare(x,y);

        Comparator<Integer> com1 = Integer::compare;
    }

    /**
     * 对象::实列方法名
     */
    @Test
    public void test1(){
        Consumer<String> con =(x) -> System.out.println(x);

        PrintStream ps = System.out;
        Consumer<String> con1  = ps::println;

        Consumer<String> con2 = System.out::println;
        con2.accept("abcdef");
    }

    @Test
    public void test2(){
        Employee emp = new Employee();
        Supplier<String> sup = () -> emp.getName();
        String str = sup.get();
        System.out.println(str);

        Supplier<Integer> sup2 =emp::getAge;
        Integer num = sup2.get();
        System.out.println(num);
    }
}
